package project.dade.cn.bst.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.Theme;
import com.alibaba.fastjson.JSON;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import project.dade.cn.bst.MainActivity;
import project.dade.cn.bst.R;
import project.dade.cn.bst.activity.ChooseTimeActivity;
import project.dade.cn.bst.activity.MyComment;
import project.dade.cn.bst.activity.MyFeedActivity;
import project.dade.cn.bst.activity.MyaddressActivity;
import project.dade.cn.bst.activity.MyorderActivity;
import project.dade.cn.bst.base.ParentFragment;
import project.dade.cn.bst.base.ZJPbaseFragment;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.manager.AppApplication;
import project.dade.cn.bst.model.BaseRequestsModel;
import project.dade.cn.bst.model.CommentModel;
import project.dade.cn.bst.model.IntegralModel;
import project.dade.cn.bst.model.MyfeedsDate;
import project.dade.cn.bst.network.FormatRequest;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.AppUtil;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.views.EmptyLayout;

/**
 * Created by vain zhou on 2015/9/25.
 */
public class MyFragment extends ParentFragment implements
        View.OnClickListener {
    public static String FRAGMENT_TAG = MyFragment.class.getSimpleName();
    @InjectView(R.id.layout_myaddress)
    RelativeLayout myaddress;
    @InjectView(R.id.layout_myfeeds)
    RelativeLayout myfeeds;
    @InjectView(R.id.layout_myorders)
    RelativeLayout myorders;
    @InjectView(R.id.layout_mycomments)
    RelativeLayout mycomments;
    @InjectView(R.id.exitBtn)
    Button exit;
    @InjectView(R.id.tv_user_balance)
    TextView tv_name;
    @InjectView(R.id.tv_Integrations)
    TextView tv_integration;
    private KProgressHUD hud;


    @Override
    public int setRootViewID() {
        return 0;
    }

    @Override
    public void findView(View rootView) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_my, null);
        ButterKnife.inject(this, v);
        myaddress.setOnClickListener(this);
        myfeeds.setOnClickListener(this);
        myorders.setOnClickListener(this);
        mycomments.setOnClickListener(this);
        exit.setOnClickListener(this);
        return v;
    }
    @Override
    public void onResume() {
        super.onResume();
        initRequest();
    }


    private void initRequest() {
        if (AppUtil.isNetworkAvailable(getActivity())) {
            requestGetintegral();
            tv_name.setText(AppApplication.getInstance().getSpUtil().getUSER_mobile());

        } else {
            ToastUtil.showShort(getActivity(), R.string.tips_network_error);
        }
    }

    private void requestGetintegral() {
        hud = KProgressHUD.create(getActivity())
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在加载")
                .setCancellable(true);
        hud.show();
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", AppApplication.getInstance().getSpUtil().getUserID().toString());
        params.put("mobilephone", AppApplication.getInstance().getSpUtil().getUSER_mobile());
        params.put("Sign", FormatRequest.main(params));
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.Integration).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        hud.dismiss();
                        ToastUtil.showShort(getActivity(), "网络相应失败请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {
                        hud.dismiss();
                        LogUtil.i(TAG, "获取个人积分：" + response);
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取个人积分"))) {
                            IntegralModel model = JSON.parseObject(response, IntegralModel.class);
                            //获取请求成功
                            if (model.getResult() == 0) {
                                tv_integration.setText(model.getData().getPoints()+"");
                            }else {
                                tv_integration.setText(0+"");

                            }

                        } else {
                            hud.dismiss();
                            ToastUtil.showShort(getActivity(), NetRequestUtil.requestsError(response, "获取个人积分"));
                            tv_integration.setText("0");


                        }
                    }

                });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_myaddress:
                Intent intent= new Intent(getActivity(), MyaddressActivity.class);
                intent.putExtra("mark","3");
                startActivity(intent);
            break;
            case R.id.layout_myfeeds:
                redirectToPage(MyFeedActivity.class);
                break;
            case R.id.layout_myorders:
                Intent intent3 = new Intent();
                intent3.setClass(getActivity(), MyorderActivity.class);
                startActivity(intent3);
                break;
            case R.id.layout_mycomments:
                redirectToPage(MyComment.class);
                break;
            case R.id.exitBtn:
                doClearUserInfo();
                break;
        }

    }
    /**
     * 跳转相关页功能
     *
     * @param <T>
     */
    private <T> void redirectToPage(Class<T> class1) {

        startActivity(new Intent(getActivity(), class1));
    }

    /**
     * 退出登录清除个人信息
     */
    private void doClearUserInfo() {
        new MaterialDialog.Builder(getActivity())
                .title(R.string.tips_title)
                .titleColor(getResources().getColor(R.color.background_material_dark))
                .theme(Theme.LIGHT)
                .content(R.string.tips_connect_exit_hit)
                .positiveText(R.string.ok)
                .negativeText(R.string.cancel)
                .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onPositive(MaterialDialog dialog) {
                        //确定退出账号
                        AppApplication.getInstance().getSpUtil().setLogin(false);
                        AppApplication.getInstance().getSpUtil().setUserID(0);
                        AppApplication.getInstance().getSpUtil().setUSER_mobile("");
                        MainActivity.instance.initOption();

                    }
                }).show();
    }
}
